/**
 * Copyright (C) 2011, mordredd666 [_at_] gmail.com
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *  
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *  
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package de.mordred.gui.lernen;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import de.mordred.Frage;
import de.mordred.gui.AntwortenPanel;
import de.mordred.gui.FixedPanel;
import de.mordred.gui.FragePanel;
import de.mordred.gui.TopLeiste;

public class LernenFrageGui extends FixedPanel
{
	private static final long serialVersionUID = -6262051369247643631L;
	private FragePanel _frage;
	private AntwortenPanel _antworten;

	private JButton _leftarrow;
	private JButton _rightarrow;
	private JLabel _fortschritt = new JLabel("0 / 0");
	private ArrayList<Frage> _fragen;
	private Frage _currentFrage;
	private int _currentFrageNummer;

	public LernenFrageGui()
	{
		JPanel northpanel = new JPanel();
		_leftarrow = new JButton("<--");
		_leftarrow.addActionListener(changeFrage(false));
		_rightarrow = new JButton("-->");
		_rightarrow.addActionListener(changeFrage(true));

		_frage = new FragePanel();
		northpanel.setLayout(new GridBagLayout());
		northpanel.add(new TopLeiste(), new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0,
				GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 1, 1));

		northpanel.add(_leftarrow, new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0,
				GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 1, 1));
		northpanel.add(_rightarrow, new GridBagConstraints(3, 1, 1, 1, 1.0, 1.0,
				GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 1, 1));
		northpanel.add(_fortschritt, new GridBagConstraints(2, 1, 1, 1, 1.0, 1.0,
				GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5),
				1, 1));

		northpanel.add(_frage, new GridBagConstraints(0, 2, 4, 1, 1.0, 1.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 1, 1));

		_antworten = new AntwortenPanel(false);

		this.setLayout(new BorderLayout());
		this.add(northpanel, BorderLayout.NORTH);
		this.add(_antworten, BorderLayout.CENTER);

	}

	public LernenFrageGui(ArrayList<Frage> fragen, boolean shuffle)
	{
		this();

		_fragen = fragen;

		if (shuffle)
		{
			Collections.shuffle(_fragen);
		}

		_fortschritt.setText(_currentFrageNummer + 1 + " / " + fragen.size());

		_currentFrage = _fragen.get(_currentFrageNummer);

		setFrage(_currentFrage);
		this.invalidate();
		this.repaint();
		this.revalidate();
	}

	public void setFrage(Frage frage)
	{
		if (frage.hatBild())
		{
			_frage.setImage(new ImageIcon(frage.getBild()));
		}
		else
		{
			_frage.setImage(null);
		}
		_frage.setText(frage.getFragetext());

		_fortschritt.setText(_currentFrageNummer + 1 + " / " + _fragen.size());

		_antworten.setAntworten(frage);

		this.repaint();
		this.revalidate();

	}

	@Override
	public String getTitel()
	{
		return "Fragen";
	}

	public ActionListener changeFrage(final boolean up)
	{
		return new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent e)
			{
				boolean changed = false;

				if (up && _currentFrageNummer < _fragen.size() - 1)
				{
					_currentFrageNummer++;
					changed = true;

				}
				else if (!up && _currentFrageNummer > 0)
				{
					_currentFrageNummer--;
					changed = true;
				}

				if (changed)
				{
					_currentFrage = _fragen.get(_currentFrageNummer);
					setFrage(_currentFrage);
				}
			}
		};

	}

}
